package com.bpz.jxw.article.controller;

import com.alibaba.fastjson.JSON;
import com.bpz.jxw.article.model.Article;
import com.bpz.jxw.article.service.IArticleService;
import com.bpz.main.bean.ArticleInterval;
import com.bpz.main.model.ActMsg;
import com.bpz.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 半瓶子 on 2/8/15.
 * blog: xusir.cnblogs.com
 */
@Controller
public class ArticleCtrl {
    @Autowired
    IArticleService articleService;
    public static final int pageSize = 20;

    @RequestMapping(value = {"/"})
    public ModelAndView index() {
        return getArticles("hot", 1, -1, "");
    }

    @RequestMapping(value = "a/{interval}/{pageIndex}", method = RequestMethod.GET)
    public ModelAndView articles(@PathVariable String interval, @PathVariable int pageIndex) {
        return getArticles(interval, pageIndex, -1, "");
    }

    @RequestMapping(value = "a/{interval}", method = RequestMethod.GET)
    public ModelAndView articles(@PathVariable String interval) {

        return getArticles(interval, 1, -1, "");
    }

    @RequestMapping(value = "a/pic", method = RequestMethod.GET)
    public ModelAndView picArt() {
        return getArticles("late", 1, 1, "pic");
    }

    @RequestMapping(value = "a/pic/{pageIndex}", method = RequestMethod.GET)
    public ModelAndView picArt(@PathVariable int pageIndex) {
        return getArticles("late", pageIndex, 1, "pic");

    }

    @RequestMapping(value = "a/video", method = RequestMethod.GET)
    public ModelAndView videoArt() {
        return getArticles("late", 1, 2, "video");

    }

    @RequestMapping(value = "a/video/{pageIndex}", method = RequestMethod.GET)
    public ModelAndView videoArt(@PathVariable int pageIndex) {
        return getArticles("late", pageIndex, 2, "video");

    }

    @RequestMapping(value = "article/{id}", method = RequestMethod.GET)
    public ModelAndView article(@PathVariable int id) {
        ModelAndView view = new ModelAndView();
        List<Article> articles = articleService.getNextAndPrev(id);
        Map<String, Object> map = new HashMap<String, Object>();
        int size = articles.size();

        if (articles != null && size > 0) {
            Article article = articles.get(0);
            if (size == 1) {
                map.put("data", article);
            } else if (size == 2) {
                if (article.getId().equals(id)) {
                    map.put("data", article);
                    map.put("next", articles.get(1).getId());
                } else {
                    map.put("data", articles.get(1));
                    map.put("prev", articles.get(0).getId());
                }
            } else {
                map.put("prev", articles.get(0).getId());
                map.put("data", articles.get(1));
                map.put("next", articles.get(2).getId());
            }
        }
        view = new ModelAndView("main", "article", map);
        view.addObject("template", "/include/article.html");
        return view;
    }

    private ModelAndView getArticles(String interval, int pageIndex, int type, String typeName) {
        int hour = 8;
        ModelAndView view;
        try {
            ArticleInterval articleInterval = Enum.valueOf(ArticleInterval.class, interval);
            if (articleInterval != null) {
                hour = articleInterval.getValue();
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("pageIndex", pageIndex);
            map.put("pageSize", pageSize);
            map.put("interval", hour);
            if (type > 0 && !StringUtils.isEmpty(typeName)) {
                map.put("intervalType", typeName);
            } else {
                map.put("intervalType", articleInterval.name());
            }
            map.put("contentType", type);
            view = new ModelAndView("main", "article", map);
            view.addObject("template", "/include/articles.ftl");
        } catch (Exception ex) {
            view = new ModelAndView("main");
            view.addObject("template", "/error/404.ftl");
        }
        return view;
    }

    @RequestMapping(value = "article/comments/vote_to", method = RequestMethod.POST)
    @ResponseBody
    public String voteTo(@RequestParam(value = "id", required = true, defaultValue = "0") int id, @RequestParam(value = "action", required = true, defaultValue = "0") int action) {
        ActMsg msg = articleService.voteTo(id, action);
        return JSON.toJSONString(msg);
    }
}
